//初始化数据
$(function () {
    getData(1,6);
})
//更新数据
function getData(pageNo,length) {
    $.ajax({
        url: "/route/list",
        type: "POST",
        dataType: "json",
        data:getSearch(pageNo,length),
        success:function (json) {
            var str="";
            var dataList = json.data.list;
            createPageNav({
                $container : $("#pageing1"),
                pageCount :json.data.pages,
                currentNum : json.data.pageNum,
                afterFun : function(num){
                    getData(num,6);
                },
                hasCommonPage:false
            });

            for (var i = 0; i < dataList.length; i++) {
                var routeType =dataList[i].routeType;
                if (routeType ==0){routeType="省内路线"}
                if (routeType ==1){routeType="省外路线"}
                if (routeType ==2){routeType="国外路线"}
                var date = "";
                date = new Date(dataList[i].gmtModify);
                str += "<tr>"
                    + '<th>' + dataList[i].id + '</th>'
                    +"<th ><img src='"+dataList[i].img+"' class='routeImg'></th>"
                    + "<th>" + dataList[i].routeName + "</th>"
                    + "<th>" + routeType + "</th>"
                    + "<th>" + formatDate(dataList[i].updateTime)+ "</th>"
                    + "<th >"
                    +"<input type='button' value='生成活动'  onclick='addById("+dataList[i].id+")'  class='btn btn btn-success btn-sm'/>"
                    +"</th>"
                    + "</tr>"
            }
            $('#list').html(str);
        },
        error:function () {
            alert("操作失败");
        }
    })
}
//生成活动
function addById(id) {
    $.ajax({
        url: "/activity/add",
        type: "POST",
        dataType: "json",
        data: {id: id},
        success: function (json) {
            alert("生成成功")
        },
        error: function (json) {
            alert("生成失败")
        }
    })
}

//获取检索条件
function getSearch(pageNo,length) {
    var routeName = $('[name="routeName"]').val();
    var routeType = $('[name="routeType"]').val();
    var starttime = $('[name="starttime"]').val();
    var endtime = $('[name="endtime"]').val();
    var parm={pageNo:pageNo,length:length,routeName:routeName,routeType:routeType,starttime:starttime,endtime:endtime};
    return parm;
}
//时间戳转系统时间格式
function formatDate(now) {
    if (now==""){return "";}
    return new Date(now).toLocaleString();
}